<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto layout-padding-view">
			<div class="system-user-search mb15">
				<el-input size="default" class="w_100 mr10" v-model="listQuery.keyWord" placeholder="模糊查询" @keydown.enter="searchForm"> </el-input>
				<el-select size="default" v-model="listQuery.OperationType" placeholder="日志类型" class="w_100 mr10" clearable @change="searchForm">
					<el-option v-for="item in state.functionOptions" :key="item.enumValue" :label="item.desction" :value="item.enumValue"></el-option>
				</el-select>
				<el-select size="default" v-model="listQuery.SysRoleId" placeholder="操作角色" class="w_100 mr10" clearable @change="searchForm">
					<el-option v-for="item in state.roleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
				<el-select size="default" v-model="listQuery.Status" placeholder="操作状态" class="w_100" clearable @change="searchForm">
					<el-option label="失败" value="1" />
					<el-option label="成功" value="0" />
				</el-select>
				<el-date-picker
					v-model="state.dateArr"
					type="daterange"
					size="default"
					range-separator="至"
					start-placeholder="开始日期"
					class="w_220 ml10"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					end-placeholder="结束日期"
					align="right"
					@change="searchForm"
				/>
				<el-button size="default" @click="searchForm" type="primary" class="ml10"> 查询 </el-button>
				<el-button size="default" type="info" class="ml10" @click="resetForm"> 重置 </el-button>
			</div>
			<tableTem ref="tableRef" v-bind:="state.tableData" @pageChange="onTablePageChange">
				<el-table-column align="center" type="index" label="序号" width="60" />
				<el-table-column align="center" min-width="100" prop="operationTypeName" label="日志类型"></el-table-column>
				<el-table-column align="center" min-width="120" prop="currentAccountNumber" label="操作账号"></el-table-column>
				<el-table-column align="center" min-width="120" prop="currentUserName" label="操作用户"></el-table-column>
				<el-table-column align="center" min-width="120" prop="currentRoleName" label="操作角色"></el-table-column>
				<el-table-column align="center" min-width="150" prop="apiDescription" label="操作描述"></el-table-column>
				<el-table-column align="center" min-width="90" prop="statusName" label="操作状态"></el-table-column>
				<el-table-column align="center" show-overflow-tooltip min-width="120" prop="exceptionMessage" label="错误信息"></el-table-column>
				<el-table-column align="center" min-width="130" prop="ipAddress" label="操作IP"></el-table-column>
				<el-table-column align="center" min-width="120" prop="createTime" fixed="right" label="操作时间"></el-table-column>
			</tableTem>
		</div>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { SysLogGetPagedList } from '/@/api/userInformation/index';
import { GetEnumberOptions } from '/@/api/common/index';
import { SysRoleGetPagedList } from '/@/api/role/index';
// 引入组件
const tableTem = defineAsyncComponent(() => import('/@/components/tables/index.vue'));

// 定义变量内容
const tableRef = ref();
const listQuery = reactive({
	keyWord: '',
	OperationType: '',
	SysRoleId: '',
	Status: '', // 状态
});
const state = reactive<any>({
	tableData: {
		data: [],
		total: 0,
		loading: true,
		param: {
			PageIndex: 1,
			PageSize: 10,
		},
	},
	tableTitle: [], // 列表标题
	editItem: {},
	isDisabled: false, // 详情
	dialogType: 0, // 弹窗类型
	dialogTitle: '', // 弹窗标题
	dialogVisible: false, // 弹窗状态
	dateArr: [],
	functionOptions: [], //日志类型
	roleList: [], //角色
});
// 页面加载时
onMounted(() => {
	getTableData();
	getInit();
});
// 初始化表格数据
const getInit = async () => {
	let data = await GetEnumberOptions({ enumName: 'FunctionOptionsEnum' });
	state.functionOptions = data.data;
	let dataRole = await SysRoleGetPagedList({
		PageIndex: 1,
		PageSize: 99999,
	});
	state.roleList = dataRole.data.items;
};
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const params = {
			...state.tableData.param,
			...listQuery,
		};
		if (Array.isArray(state.dateArr) && state.dateArr.length > 0) {
			params.StartCreateTime = `${state.dateArr[0]} 00:00:00`;
			params.EndCreateTime = `${state.dateArr[0]} 23:59:59`;
		}
		console.log(params);

		Object.keys(params).forEach((key) => {
			if (!params[key]) delete params[key];
		});
		let data = await SysLogGetPagedList(params);

		state.tableData.data = data.data.items;
		state.tableData.total = data.data.totalCount;
	} finally {
		state.tableData.loading = false;
	}
};
// 弹窗打开
const openDialog = (title: any, type: any) => {
	state.dialogTitle = title;
	state.dialogType = type;
	state.dialogVisible = true;
};

// 详情
const getDetail = (v: any) => {
	const labelsWithValues = state.tableTitle.reduce((acc: any, item: any) => {
		acc[item.fieldName] = v[item.fieldName];
		return acc;
	});

	state.editItem = labelsWithValues;
	console.log(state.editItem);

	state.isDisabled = true;
	openDialog('详情', 1);
};

// 关闭弹框
const closeDialog = () => {
	state.dialogTitle = '';
	state.dialogType = 0;
	state.dialogVisible = false;
	state.editItem = {};
	state.isDisabled = false;
	searchForm();
};

// 分页改变
const onTablePageChange = (val: any) => {
	state.tableData.param.PageIndex = val.pageNum;
	state.tableData.param.PageSize = val.pageSize;
	getTableData();
};
// 搜索点击时表单回调
const searchForm = () => {
	state.tableData.param.PageIndex = 1;
	tableRef.value.pageReset();
};
// 重置
const resetForm = () => {
	listQuery.keyWord = '';
	listQuery.OperationType = '';
	listQuery.Status = '';
	listQuery.SysRoleId = '';
	state.dateArr = [];
	searchForm();
};
</script>

<style scoped lang="scss">
.system-role-container {
	.system-role-padding {
		padding: 15px;
		.el-table {
			flex: 1;
		}
	}
}
</style>
